package io.github.wppli.infrastructure.persistent.repository;

import io.github.wppli.domain.recommond.model.entity.ContentFeatureEntity;
import io.github.wppli.domain.recommond.repository.IContentFeatureRepository;
import io.github.wppli.infrastructure.persistent.dao.IContentFeatureDao;
import io.github.wppli.infrastructure.persistent.po.ContentFeature;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author li--jiaqiang 2025−03−15
 */
@Repository
@AllArgsConstructor
public class ContentFeatureRepository implements IContentFeatureRepository {

    private final IContentFeatureDao contentFeatureDao;

    @Override
    public List<ContentFeatureEntity> findByContentIds(List<Long> contentIds) {
        if (contentIds == null || contentIds.isEmpty()) {
            return Collections.emptyList();
        }
        List<ContentFeature> contentFeatures = contentFeatureDao.selectBatchIds(contentIds);
        if (contentFeatures == null || contentFeatures.isEmpty()) {
            return Collections.emptyList();
        }
        return contentFeatures.stream().map(
                item -> new ContentFeatureEntity(item.getFeatureVector(), item.getContentId())
        ).collect(Collectors.toList());
    }

    @Override
    public List<ContentFeatureEntity> findAll() {
        List<ContentFeature> contentFeatures = contentFeatureDao.findAll();
        if (contentFeatures == null || contentFeatures.isEmpty()) {
            return Collections.emptyList();
        }
        return contentFeatures.stream().map(
                item -> new ContentFeatureEntity(item.getFeatureVector(), item.getContentId())
        ).collect(Collectors.toList());
    }

    @Override
    public void save(Long contentId, String json) {
        ContentFeature contentFeature = new ContentFeature(contentId, json);
        contentFeatureDao.insert(contentFeature);
    }
}